﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using com.gyz.hao.WinCapInfoApp.Model;
using com.gyz.hao.WinCapInfoApp.BLL;
using yeqw.FrameWork;
using System.Text.RegularExpressions;

namespace com.gyz.hao.WinCapInfoApp
{

    public class ExecCapInfoBLL
    {
        public CapInfo capInfo { set; get; }

        public void Exec()
        {
            try
            {
                int status = 0;
                switch (capInfo.pt)
                {
                    case 1:
                        status = GetYYStatus();
                        break;
                    case 2:
                        status = GetLiuJianFangStatus();
                        break;
                    case 3:
                        status = GetHuaJiaoStatus();
                        break;
                    case 4:
                        status = GetLaiFengStatus();
                        break;
                    case 5:
                        status = GetBoboStatus();
                        break;
                    case 6:
                        status = GetFanXingStatus();
                        break;
                    default:
                        return;
                }
                if (status == 4)
                {
                    AppConfigInfo.AppConfig.FailCount++;
                    return;
                }
                if (capInfo.status != status)
                {
                    MysqlDataBLL bll = new MysqlDataBLL();

                    bll.SetCapStatus(status, "id={0}".FormatExtension(capInfo.id));
                    capInfo.status = status;
                    AppConfigInfo.AppConfig.SuccessCount++;
                }
                AppConfigInfo.AppConfig.WriteCapMsg("修改{0}个状态,失败{1}个状态".FormatExtension(AppConfigInfo.AppConfig.SuccessCount, AppConfigInfo.AppConfig.FailCount));
            }
            catch (Exception ex)
            {
                AppConfigInfo.AppConfig.WriteMsg("{0}匹配出现异常,异常原因：{1}".FormatExtension(capInfo.url, ex.Message));

            }
        }

        #region YY数据判断
        /// <summary>
        /// YY数据判断
        /// </summary>
        /// <returns></returns>
        private int GetYYStatus()
        {
            int status = 4;


            WebMatchConfigInfo config = AppConfigInfo.AppConfig.GetMatchInfo(capInfo.pt);
            if (config == null)
            {
                AppConfigInfo.AppConfig.WriteMsg("YY配置信息为空:" + capInfo.pt);
                return 4;
            }
            int item = capInfo.url.LastIndexOf("/");
            string search = "";
            if (capInfo.url.LastIndexOf("?f=9101") != -1)
            {
                search = "http://www.yy.com/search?q=" + capInfo.url.Substring(item + 1, capInfo.url.Length - item - 1 - 7);
            }
            else
                search = "http://www.yy.com/search?q=" + capInfo.url.Substring(item + 1, capInfo.url.Length - item - 1);

            string htmldate = CaptureInfoHelper.SendData(search, "", config.UrlMetod, "", config.Encoding);
            if (htmldate == "")
                return 4;
            if (htmldate.IndexOf("正在开播的主播") > -1)
                return 1;//在线
            else
                return 3;//不在线
            //以下代码是临时添加 替代了下面的代码
            //string htmldate = CaptureInfoHelper.SendData(capInfo.url + "/5", "", config.UrlMetod, "", config.Encoding);


            //if (htmldate.IndexOf("不在线") > -1)
            //    return 3;//不在线
            //else
            //    return 1;







            string sid = capInfo.url.Replace("http://www.yy.com/", "").ToString();
            string html = "";
            string uid = "";
            string[] urlarr = config.Url.Split('|');
            string[] matcharr = config.Match.Split('|');
            if (urlarr.Length != 2 || urlarr.Length != matcharr.Length)
            {
                AppConfigInfo.AppConfig.WriteMsg("YYUrl和匹配配置错误:" + capInfo.pt);
                return 4;
            }
            string url;
            for (int j = 0; j < urlarr.Length; j++)
            {
                for (int i = 1; i <= AppConfigInfo.AppConfig.MaxReCount; i++)
                {
                    url = urlarr[j].Replace("{$ssid}", sid);
                    if (j == 1)
                    {
                        if (uid == "")
                        {
                            return 4;
                        }
                        string dataurl = "http://www.yy.com/{0}/{1}".FormatExtension(sid, uid);
                        html = CaptureInfoHelper.SendData(dataurl, "", config.UrlMetod, "", config.Encoding);
                        if (html.IndexOf("不在线") > -1)
                            return 3;//不在线
                        else
                            return 1;
                        url = url.Replace("{$uid}", uid);
                    }
                    // AppConfigInfo.AppConfig.WriteMsg(url + "开始时间" + System.DateTime.Now.ToString("F"));
                    html = CaptureInfoHelper.SendData(url, "", config.UrlMetod, "", config.Encoding);
                    //AppConfigInfo.AppConfig.WriteMsg(url + "结束时间" + System.DateTime.Now.ToString("F"));
                    if (html != "")
                        break;
                }
                if (html == "")
                {
                    AppConfigInfo.AppConfig.WriteMsg("YY URL抓取为空:" + capInfo.url);
                    return 4;
                }
                ResultInfo<MatchCollection> resmatch = RegexHelper.GetMatchCollectionResult(html, matcharr[j]);//正则匹配查看正则是否匹配成功
                if (resmatch.IsError)
                {
                    AppConfigInfo.AppConfig.WriteMsg("YY匹配页面失败:" + capInfo.url + "->" + html);
                    return 4;
                }
                MatchCollection mc = resmatch.ResultObject;

                if (mc.Count > 0)
                {
                    string str = mc[0].Groups["str"].Value.Trim();
                    if (j == 0)
                    {
                        uid = mc[0].Groups["str"].Value.Trim();
                    }
                    else if (j == 1)
                    {
                        if (mc[0].Groups["str"].Value.Trim() == "true")
                        {
                            status = 1;
                        }
                        else if (mc[0].Groups["str"].Value.Trim() == "false")
                        {
                            status = 3;
                        }

                    }
                }
            }
            return status;

        }
        #endregion

        #region 6间房数据判断
        private int GetLiuJianFangStatus()
        {

            int status = 4;
            WebMatchConfigInfo config = AppConfigInfo.AppConfig.GetMatchInfo(capInfo.pt);
            if (config == null)
            {

                return status;
            }
            string html = "";
            for (int i = 1; i <= AppConfigInfo.AppConfig.MaxReCount; i++)
            {
                html = CaptureInfoHelper.SendData(capInfo.url, "", "GET", "", "utf-8");
                if (html != "")
                    break;
            }
            if (html == "")
            {
                AppConfigInfo.AppConfig.WriteMsg("抓取六间房失败:" + capInfo.url);
                return status;
            }
            ResultInfo<MatchCollection> resmatch = RegexHelper.GetMatchCollectionResult(html, config.Match);
            if (resmatch.IsError)
            {
                AppConfigInfo.AppConfig.WriteMsg("匹配六间房失败:" + capInfo.url);
                return status;
            }

            MatchCollection mc = resmatch.ResultObject;
            foreach (Match m in mc)
            {
                string str = m.Groups["str"].Value.Trim();
                if (str == "" || str == "0")
                {
                    status = 3;
                }
                else
                {
                    status = 1;
                }
                //判断是否录播
                if (html.IndexOf("录像</em>") > -1)
                {
                    status = 3;
                }
                return status;

            }
            return status;
        }
        #endregion

        #region 繁星数据判断
        private int GetFanXingStatus()
        {

            int status = 4;
            WebMatchConfigInfo config = AppConfigInfo.AppConfig.GetMatchInfo(capInfo.pt);
            if (config == null)
            {
                return status;
            }
            string html = "";
            string roomid = capInfo.url.Replace("http://fanxing.kugou.com/", "");
            string url = config.Url.Replace("{$roomid}", roomid);
            for (int i = 1; i <= AppConfigInfo.AppConfig.MaxReCount; i++)
            {
                html = CaptureInfoHelper.SendData(url, "", config.UrlMetod, "", config.Encoding);
                if (html != "")
                    break;
            }
            if (html == "")
            {
                AppConfigInfo.AppConfig.WriteMsg("抓取繁星失败:" + url);
                return status;
            }
            ResultInfo<MatchCollection> resmatch = RegexHelper.GetMatchCollectionResult(html, config.Match);
            if (resmatch.IsError)
            {
                AppConfigInfo.AppConfig.WriteMsg("匹配繁星失败:" + html);
                return status;
            }

            MatchCollection mc = resmatch.ResultObject;
            foreach (Match m in mc)
            {
                string str = m.Groups["str"].Value.Trim();
                if (str == "0")
                {
                    status = 3;
                }
                else if (str == "1")
                {
                    status = 1;
                }
                else
                {
                    AppConfigInfo.AppConfig.WriteMsg("繁星状态数据异常:" + html);
                }
                return status;

            }
            return status;
        }
        #endregion

        #region 来疯数据判断
        private int GetLaiFengStatus()
        {

            int status = 4;
            WebMatchConfigInfo config = AppConfigInfo.AppConfig.GetMatchInfo(capInfo.pt);
            if (config == null)
            {
                return status;
            }
            string html = "";
            for (int i = 1; i <= AppConfigInfo.AppConfig.MaxReCount; i++)
            {
                html = CaptureInfoHelper.SendData(capInfo.url, "", "GET", "", "utf-8");
                if (html != "")
                    break;
            }
            if (html == "")
            {
                AppConfigInfo.AppConfig.WriteMsg("抓取来疯失败:" + capInfo.url);
                return status;
            }
            if (html.IndexOf("isShowing:false") > -1)
            {
                status = 3;
            }
            else if (html.IndexOf("isShowing:true") > -1)
            {
                status = 1;
            }
            else
            {
                AppConfigInfo.AppConfig.WriteMsg("匹配来疯失败:" + html);
            }

            return status;
        }
        #endregion

        #region Bobo数据判断 5
        private int GetBoboStatus()
        {

            int status = 4;
            WebMatchConfigInfo config = AppConfigInfo.AppConfig.GetMatchInfo(capInfo.pt);
            if (config == null)
            {
                return status;
            }
            string html = "";
            string roomid = capInfo.url.Replace("http://www.bobo.com/", "");
            string url = config.Url.Replace("{$roomid}", roomid);
            for (int i = 1; i <= AppConfigInfo.AppConfig.MaxReCount; i++)
            {
                html = CaptureInfoHelper.SendData(url, "", "GET", "", "utf-8");
                if (html != "")
                    break;
            }
            if (html == "")
            {
                AppConfigInfo.AppConfig.WriteMsg("抓取Bobo失败:" + url);
                return status;
            }
            ResultInfo<MatchCollection> resmatch = RegexHelper.GetMatchCollectionResult(html, config.Match);
            if (resmatch.IsError)
            {
                AppConfigInfo.AppConfig.WriteMsg("匹配Bobo失败:" + html);
                return status;
            }

            MatchCollection mc = resmatch.ResultObject;
            foreach (Match m in mc)
            {
                string str = m.Groups["str"].Value.Trim();
                if (str == "-1")
                {
                    status = 3;
                }
                else
                {
                    status = 1;
                }
                return status;
            }
            return status;
        }
        #endregion


        #region 花椒数据判断 3
        private int GetHuaJiaoStatus()
        {

            int status = 4;
            WebMatchConfigInfo config = AppConfigInfo.AppConfig.GetMatchInfo(capInfo.pt);
            if (config == null)
            {
                return status;
            }
            TimeSpan timestamp = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);//获得时间戳
            Convert.ToInt64(timestamp.TotalMilliseconds).ToString();
            string roomid = capInfo.url.Replace("http://www.huajiao.com/user/", "");//获得房间ID 
            string url = config.Url.Replace("{$roomid}", roomid);
            url = url.Replace("{$time}", Convert.ToInt64(timestamp.TotalMilliseconds).ToString());

            string html = CaptureInfoHelper.SendData(url, "", config.UrlMetod, "", config.Encoding);
            if (html == "")
            {
                AppConfigInfo.AppConfig.WriteMsg("花椒URL抓取为空:" + capInfo.url);
                return 4;
            }
            if (html.IndexOf("master") != -1)//当前页存在这个字符 说明当前主播在线
                status = 1;//在线
            else
                status = 3;//不在线
            return status;
        }
        #endregion
    }
}
